/*
 * Loongson Soc TOY Driver
 *
 * Copyright (C) 2019 Tang Haifeng <tanghaifeng-gz@loongson.cn>
 * on behalf of DENX Software Engineering GmbH
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <rtc.h>
#include <dm.h>
#include <asm/io.h>

#define REG_DELAY 30

/*RTC programmable counters 0 and 1*/
#define SYS_COUNTER_CNTRL		0x40
#define SYS_CNTRL_ERS			(1 << 23)
#define SYS_CNTRL_RTS			(1 << 20)
#define SYS_CNTRL_RM2			(1 << 19)
#define SYS_CNTRL_RM1			(1 << 18)
#define SYS_CNTRL_RM0			(1 << 17)
#define SYS_CNTRL_RS			(1 << 16)
#define SYS_CNTRL_BP			(1 << 14)
#define SYS_CNTRL_REN			(1 << 13)
#define SYS_CNTRL_BRT			(1 << 12)
#define SYS_CNTRL_TEN			(1 << 11)
#define SYS_CNTRL_BTT			(1 << 10)
#define SYS_CNTRL_E0			(1 << 8)
#define SYS_CNTRL_ETS			(1 << 7)
#define SYS_CNTRL_32S			(1 << 5)
#define SYS_CNTRL_TTS			(1 << 4)
#define SYS_CNTRL_TM2			(1 << 3)
#define SYS_CNTRL_TM1			(1 << 2)
#define SYS_CNTRL_TM0			(1 << 1)
#define SYS_CNTRL_TS			(1 << 0)

/* Programmable Counter 0 Registers */
#define SYS_TOYTRIM		0x20
#define SYS_TOYWRITE0		0x24
#define SYS_TOYWRITE1		0x28
#define SYS_TOYREAD0		0x2C
#define SYS_TOYREAD1		0x30
#define SYS_TOYMATCH0		0x34
#define SYS_TOYMATCH1		0x38
#define SYS_TOYMATCH2		0x3C

/* Programmable Counter 1 Registers */
#define SYS_RTCTRIM		0x60
#define SYS_RTCWRITE0		0x64
#define SYS_RTCREAD0		0x68
#define SYS_RTCMATCH0		0x6C
#define SYS_RTCMATCH1		0x70
#define SYS_RTCMATCH2		0x74

#define LS1X_SEC_OFFSET	(4)
#define LS1X_MIN_OFFSET	(10)
#define LS1X_HOUR_OFFSET	(16)
#define LS1X_DAY_OFFSET	(21)
#define LS1X_MONTH_OFFSET	(26)

#define TOYMATCH_SEC_OFFSET	(0)
#define TOYMATCH_MIN_OFFSET	(6)
#define TOYMATCH_HOUR_OFFSET	(12)
#define TOYMATCH_DAY_OFFSET	(17)
#define TOYMATCH_MONTH_OFFSET	(22)
#define TOYMATCH_YEAR_OFFSET	(26)

#define LS1X_SEC_MASK		(0x3f)
#define LS1X_MIN_MASK		(0x3f)
#define LS1X_HOUR_MASK	(0x1f)
#define LS1X_DAY_MASK		(0x1f)
#define LS1X_MONTH_MASK	(0x1f)
#define LS1X_YEAR_MASK	(0xffffffff)

#define ls1x_get_sec(t)	(((t) >> LS1X_SEC_OFFSET) & LS1X_SEC_MASK)
#define ls1x_get_min(t)	(((t) >> LS1X_MIN_OFFSET) & LS1X_MIN_MASK)
#define ls1x_get_hour(t)	(((t) >> LS1X_HOUR_OFFSET) & LS1X_HOUR_MASK)
#define ls1x_get_day(t)	(((t) >> LS1X_DAY_OFFSET) & LS1X_DAY_MASK)
#define ls1x_get_month(t)	(((t) >> LS1X_MONTH_OFFSET) & LS1X_MONTH_MASK)

#define RTC_CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)

struct loongson_toy_priv {
	void __iomem *base;
};

static int loongson_toy_get(struct udevice *dev, struct rtc_time *rtm)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);
	unsigned long v, t;

	v = readl(priv->base + SYS_TOYREAD0);
	t = readl(priv->base + SYS_TOYREAD1);

	memset(rtm, 0, sizeof(struct rtc_time));
	rtm->tm_year = (t & LS1X_YEAR_MASK) + 1900;
	rtm->tm_mon = ls1x_get_month(v);
	rtm->tm_mday = ls1x_get_day(v);
	rtm->tm_hour = ls1x_get_hour(v);
	rtm->tm_min = ls1x_get_min(v);
	rtm->tm_sec = ls1x_get_sec(v);

	return 0;
}

static int loongson_toy_set(struct udevice *dev, const struct rtc_time *rtm)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);
	unsigned long v, t, c;
	int ret = -1;

	v = ((rtm->tm_mon)  << LS1X_MONTH_OFFSET)
		| (rtm->tm_mday << LS1X_DAY_OFFSET)
		| (rtm->tm_hour << LS1X_HOUR_OFFSET)
		| (rtm->tm_min  << LS1X_MIN_OFFSET)
		| (rtm->tm_sec  << LS1X_SEC_OFFSET);

	writel(v, priv->base + SYS_TOYWRITE0);
	/* add timeout check counter, for more safe */
	udelay(REG_DELAY);
	c = 0x10000;
	while ((readl(priv->base + SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
		udelay(1000);

	if (!c) {
		debug("set time timeout!\n");
		goto err;
	}

	t = rtm->tm_year - 1900;
	writel(t, priv->base + SYS_TOYWRITE1);
	udelay(REG_DELAY);
	c = 0x10000;
	while ((readl(priv->base + SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
		udelay(1000);

	if (!c) {
		debug("set time timeout!\n");
		goto err;
	}
	return 0;
err:
	return ret;
}

static int loongson_alarm_get(struct udevice *dev, struct rtc_time *rtm)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);
	unsigned long v, t;

	v = readl(priv->base + SYS_TOYMATCH0);
	t = readl(priv->base + SYS_TOYREAD1);

	memset(rtm, 0, sizeof(struct rtc_time));
	rtm->tm_year = t & ~0x3f;
	rtm->tm_year = (rtm->tm_year | ((v >> TOYMATCH_YEAR_OFFSET) & 0x3f)) + 1900;
	rtm->tm_mon = ((v >> TOYMATCH_MONTH_OFFSET) & LS1X_MONTH_MASK);
	rtm->tm_mday = ((v >> TOYMATCH_DAY_OFFSET) & LS1X_DAY_MASK);
	rtm->tm_hour = ((v >> TOYMATCH_HOUR_OFFSET) & LS1X_HOUR_MASK);
	rtm->tm_min = ((v >> TOYMATCH_MIN_OFFSET) & LS1X_MIN_MASK);
	rtm->tm_sec = ((v >> TOYMATCH_SEC_OFFSET) & LS1X_SEC_MASK);

	return 0;
}

static int loongson_alarm_set(struct udevice *dev, const struct rtc_time *rtm)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);
	unsigned long v;

	v = (((rtm->tm_year - 1900) & 0x3f)  << TOYMATCH_YEAR_OFFSET)
		| ((rtm->tm_mon)  << TOYMATCH_MONTH_OFFSET)
		| (rtm->tm_mday << TOYMATCH_DAY_OFFSET)
		| (rtm->tm_hour << TOYMATCH_HOUR_OFFSET)
		| (rtm->tm_min  << TOYMATCH_MIN_OFFSET)
		| (rtm->tm_sec  << TOYMATCH_SEC_OFFSET);

	writel(v, priv->base + SYS_TOYMATCH0);

	return 0;
}

static int loongson_toy_reset(struct udevice *dev)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);
	unsigned long v;
	int ret;

	v = readl(priv->base + SYS_COUNTER_CNTRL);
	writel(v | 0x0900, priv->base + SYS_COUNTER_CNTRL);
	udelay(REG_DELAY);
	v = readl(priv->base + SYS_COUNTER_CNTRL);
	if (!(v & RTC_CNTR_OK)) {
		printf("rtc counters not working\n");
		ret = -1;
		goto err;
	}
	ret = -1;
	/* set to 1 HZ if needed */
	udelay(REG_DELAY);
	if (readl(priv->base + SYS_TOYTRIM) != 32767) {
		v = 0x100000;
		udelay(REG_DELAY);
		while ((readl(priv->base + SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS) && --v)
			udelay(1000);

		if (!v) {
			printf("time out\n");
			goto err;
		}
		writel(32767, priv->base + SYS_TOYTRIM);
	}
	/* this loop coundn't be endless */
	udelay(REG_DELAY);
	while (readl(priv->base + SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS)
		udelay(1000);

	return 0;
err:
	return ret;
}

static int loongson_toy_probe(struct udevice *dev)
{
	struct loongson_toy_priv *priv = dev_get_priv(dev);

	priv->base = dev_remap_addr(dev);
	if (!priv->base)
		return -EINVAL;

	return 0;
}

static const struct rtc_ops loongson_toy_ops = {
	.get = loongson_toy_get,
	.set = loongson_toy_set,
	.reset = loongson_toy_reset,
	.alarm_get = loongson_alarm_get,
	.alarm_set = loongson_alarm_set,
};

static const struct udevice_id loongson_toy_ids[] = {
	{ .compatible = "loongson,soc-toy" },
	{ }
};

U_BOOT_DRIVER(toy_loongson) = {
	.name	= "toy-loongson",
	.id	= UCLASS_RTC,
	.probe	= loongson_toy_probe,
	.of_match = loongson_toy_ids,
	.ops	= &loongson_toy_ops,
	.priv_auto_alloc_size = sizeof(struct loongson_toy_priv),
};
